#include <iostream>
#include <stdlib.h>

#include "gtest/gtest.h"

#include "kpextVkContext.h"
#include "KpExtGP.h"
#include "kpextOpImgCvtF32C1toF32C2.h"

#include "kpextCtxGlobal.h"

using namespace std;

TEST(kpextFuncTest, OpImgCvtF32C1toF32C2){
    ASSERT_NE(nullptr, GvkContextPtr);

    int cols = 1024, rows = 500;

    auto cmdbuf = GvkContextPtr->cmdbuf();

    size_t size = static_cast<size_t>(cols * rows);
    auto stagebuf1 = GvkContextPtr->stagingBuf(size * 4 * 2);
    auto stagebuf2 = GvkContextPtr->stagingBuf(size * 4 * 2);
    auto inbuf = GvkContextPtr->deviceBuf(size * 4 * 2);
    auto outbuf = GvkContextPtr->deviceBuf(size * 4 * 2);
    auto op = std::make_shared<kpext::OpImgCvtF32C1toF32C2>(
                GvkContextPtr
                );

    ASSERT_NE(nullptr, cmdbuf);
    ASSERT_NE(nullptr, stagebuf1);
    ASSERT_NE(nullptr, stagebuf2);
    ASSERT_NE(nullptr, inbuf);
    ASSERT_NE(nullptr, outbuf);
    ASSERT_NE(nullptr, op);


    {
        auto data = stagebuf1->data<float>();
        for (size_t i = 0; i < size; ++i) {
            data[i] = static_cast<float>(i);
        }
        stagebuf1->unmap();
    }


    kpext::CmdBaseBufferCpy bufcpy;
    kpext::CmdBaseBufferSet bufset;
    kpext::CmdBuffersMemBarrier barrierTx;
    kpext::CmdBuffersMemBarrier barrierRx;

    barrierTx.prepare(
                {inbuf, outbuf},
                vk::PipelineStageFlagBits::eTransfer,
                vk::PipelineStageFlagBits::eComputeShader,
                vk::AccessFlagBits::eTransferWrite,
                vk::AccessFlagBits::eShaderRead
                );
    barrierRx.prepare(
                {inbuf, outbuf},
                vk::PipelineStageFlagBits::eComputeShader,
                vk::PipelineStageFlagBits::eTransfer,
                vk::AccessFlagBits::eShaderWrite,
                vk::AccessFlagBits::eTransferRead
                );

    op->rebuild(cols, rows,
                inbuf, outbuf
                );

    cmdbuf->begin();
    bufcpy.prepare(inbuf, stagebuf1, size * 4);
    cmdbuf->record(&bufcpy);
    cmdbuf->record(&barrierTx);

    cmdbuf->record(op.get());

    bufcpy.prepare(stagebuf2, outbuf);
    cmdbuf->record(&barrierRx);
    cmdbuf->record(&bufcpy);

    cmdbuf->submit();
    cmdbuf->waitExec();

    {
        auto p1 = stagebuf1->data<float>();
        auto p2 = stagebuf2->data<float>();
//            SPDLOG_INFO(__FUNCTION__ " output: [{}] ", stagebuf2->vector<float>());
        bool isalleq = true;
        for (int r = 0; r < rows; ++r) {
            float * sptr = p2;
            for (int c = 0; c < cols; ++c) {
                int idx = c + r * cols;
                if(c == 0){
                    sptr = p2 + idx * 2;
                }
                float f1 = p1[idx];
                float f2 = p2[idx * 2];
                isalleq = isalleq && ( f1 == f2);
            }
//            std::vector<float> rowdisp(sptr, sptr + cols * 2);
//            SPDLOG_INFO(" {} ", rowdisp);
        }
        stagebuf1->unmap();
        stagebuf2->unmap();
        EXPECT_EQ(true, isalleq);
    }

}


GTEST_API_ int main(int argc, char **argv) {
    printf("Running main() from %s\n", __FILE__);
    //    kpextSetLoggerLevel(KpExtLOG_LEVEL_DEBUG);
    kpextSetLoggerLevel(KpExtLOG_LEVEL_INFO);
    //  kpextSetLoggerPattern("[%D %H:%M:%S.%e] [%^%l%$] %v");
    kpextSetLoggerPattern("   [SPDLOG_%l] %v");

    testing::InitGoogleTest(&argc, argv);

    testing::Environment* const foo_env =
            testing::AddGlobalTestEnvironment(new KpExtCtxEnv);
    UNUSE_VAR(foo_env);

    return RUN_ALL_TESTS();
}
